package auth

import (
	"log"
	"net/http"

	"gitee.com/zoneaurora/shop/internal/common"
	"gitee.com/zoneaurora/shop/internal/model"
	"github.com/gin-gonic/gin"
)

func Login(c *gin.Context) {
  db := common.DB()

  // 获取请求参数
  var requestUser model.User
  c.Bind(&requestUser)

	// 验证参数数据
	if len(requestUser.Username) < 6 {
		common.JSON(c, http.StatusUnprocessableEntity,
			"用户名不能少于6位", nil)
		return
	}
	if len(requestUser.Password) < 6 {
		common.JSON(c, http.StatusUnprocessableEntity,
			"密码不能少于6位", nil)
		return
	}

	// 判断用户是否存在
	var user model.User
  err := db.Where("username = ?", requestUser.Username).First(&user).Error
	if err != nil {
		common.JSON(c, http.StatusUnprocessableEntity,
			"用户不存在", nil)
		return
	}

  // 构建token
	token, err := common.GenToken(user)
	if err != nil {
		common.JSON(c, http.StatusInternalServerError,
			"系统异常", nil)
		log.Panicln(err)
		return
	}


  common.JSON(c, http.StatusOK,"登录成功", gin.H{
    "token": token,
  })
}

func Register(c *gin.Context) {
	db := common.DB()

  // 获取请求参数
  var requestUser model.User
  c.Bind(&requestUser)

	// 验证参数数据
	if len(requestUser.Username) < 6 {
		common.JSON(c, http.StatusUnprocessableEntity,
			"用户名不能少于6位", nil)
		return
	}
	if len(requestUser.Password) < 6 {
		common.JSON(c, http.StatusUnprocessableEntity,
			"密码不能少于6位", nil)
		return
	}

	// 判断用户是否存在
	var user model.User
  err := db.Where("username = ?", requestUser.Username).First(&user).Error
	if err == nil {
		common.JSON(c, http.StatusUnprocessableEntity,
			"用户已存在", nil)
		return
	}

  // 创建用户
  if err := db.Create(&requestUser).Error; err != nil {
		common.JSON(c, http.StatusInternalServerError,
			"系统异常", nil)
		log.Panicln(err)
		return
  }

  // 构建token
	token, err := common.GenToken(requestUser)
	if err != nil {
		common.JSON(c, http.StatusInternalServerError,
			"系统异常", nil)
		log.Panicln(err)
		return
	}

  common.JSON(c, http.StatusOK,"用户注册成功", gin.H{
    "token": token,
  })
}

func UserInfo(c *gin.Context) {
  var userModel model.User
  user, ok := c.Get("user")
  if !ok {
		common.JSON(c, http.StatusInternalServerError,
			"系统异常", nil)
		return
  }
  userModel = user.(model.User)
  common.JSON(c, http.StatusOK,
    "用户信息获取成功", userModel)
}

func EditUserInfo(c *gin.Context) {
  db := common.DB()

  // 获取请求参数
  var requestUser model.User
  c.Bind(&requestUser)

	if len(requestUser.Telephone) != 11 {
		common.JSON(c, http.StatusBadRequest,
			"手机号必须是11位", nil)
		return
	}

  var user model.User
  if db.First(&user, requestUser.ID).
      Updates(model.User{
        Address: requestUser.Address, 
        Telephone: requestUser.Telephone,
      }).Error != nil {
		common.JSON(c, http.StatusInternalServerError,
			"系统异常", nil)
		return
  }
  common.JSON(c, http.StatusOK, "更新成功", nil)
}

func ChargeMoney(c *gin.Context) {
  db := common.DB()

  // 获取请求参数
  var requestUser model.User
  c.Bind(&requestUser)

  var user model.User
  if db.First(&user, requestUser.ID).
      Update("money", user.Money + requestUser.Money).Error != nil {
		common.JSON(c, http.StatusInternalServerError,
			"系统异常", nil)
		return
  }
  common.JSON(c, http.StatusOK, "充值成功", nil)
}
